<?php

class Pictet2_Thumbnailer_Gd extends Pictet2_Thumbnailer_Abstract {
	
	private $_validTypes = array();
	
	public function __construct() {
		$this->_validTypes = array("jpg","gif","png");
	}
	
	public function isValid($file) {
		$info = pathinfo($file);
		return in_array($info["extension"], $this->_validTypes);
	}
	
	public function makeThumb($name) {
		if(!file_exists(".thumbs")) {
			mkdir(".thumbs");
		}
		$filename = ".thumbs" . DS . $name;
		$new_w = (Pictet2_Settings::getSetting("thumbWidth")) ? Pictet2_Settings::getSetting("thumbWidth") : 150;
		$new_h = (Pictet2_Settings::getSetting("thumbHeight")) ? Pictet2_Settings::getSetting("thumbHeight") : 150;
		
		if(file_exists($name)) {
			$system=explode(".",$name);
			$system=array_reverse($system);
	
			if (preg_match("/jpg|jpeg|JPG/",$system[0])){ $src_img=imagecreatefromjpeg($name); if(!$src_img) return false; }
			if (preg_match("/png/",$system[0])){ $src_img=imagecreatefrompng($name); if(!$src_img) return false; }
			if (preg_match("/gif/",$system[0])){ $src_img=imagecreatefromgif($name); if(!$src_img) return false; }
	
			$old_x=imageSX($src_img);
			$old_y=imageSY($src_img);
			if ($old_x > $old_y) {
				$thumb_w=$new_w;
				$thumb_h=$old_y*($new_h/$old_x);
			}
			if ($old_x < $old_y) {
				$thumb_w=$old_x*($new_w/$old_y);
				$thumb_h=$new_h;
			}
			if ($old_x == $old_y) {
				$thumb_w=$new_w;
				$thumb_h=$new_h;
			}
			$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
			if (preg_match("/png|PNG/",$system[0])){
				imagepng($dst_img,$filename);
			} elseif(preg_match("/jpg|jpeg|JPG/",$system[0])) {
				imagejpeg($dst_img,$filename,88);
			} else {
				imagegif($dst_img,$filename);
			}
			imagedestroy($dst_img);
			imagedestroy($src_img);
		}
	}
	
	public function directoryPreview($img_name, $dir) {
		if(!file_exists($dir . DS . ".thumbs")) {
			mkdir($dir . DS . ".thumbs");
		}
		$size = 100;
		$parts = pathinfo($img_name);
		$name = $dir . DS . ".thumbs" . DS . ".dirpreview.jpg";
		if (preg_match("/jpg|jpeg|JPG/",$parts["extension"])){ $img=imagecreatefromjpeg($dir . DS . $img_name); if(!$img) return false; }
		if (preg_match("/png/",$parts["extension"])){ $img=imagecreatefrompng($dir . DS . $img_name); if(!$img) return false; }
		if (preg_match("/gif/",$parts["extension"])){ $img=imagecreatefromgif($dir . DS . $img_name); if(!$img) return false; }
		
		$img_info = getimagesize($dir . DS . $img_name);
	
		list($width, $height) = $img_info;

		if($width > $height) {
			$in = round(($width-$height)/2);
			$sq = ImageCreateTrueColor($size, $size);
			imagecopyresampled($sq, $img, 0, 0, $in, 0, $size, $size, $height, $height);
			imagejpeg($sq, $name);
		} else {
			$in = round(($height-$width)/2);
			$sq = ImageCreateTrueColor($size, $size);
			imagecopyresampled($sq, $img, 0, 0, 0, $in, $size, $size, $width, $width);
			imagejpeg($sq, $name, 88);
		}
	
		imagedestroy($sq);
		imagedestroy($img);
	}
	
}